/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dvd.view;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 16894552-3
 */
public class AddDVDFormServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        List errorMsgs = (List) request.getAttribute("error");
        
        String title=request.getParameter("title");
        String year=request.getParameter("year");
        String genre=request.getParameter("genre");
        String newgenre=request.getParameter("newGenre");
        
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>FORMULARIO INGRESO DE DVD</title>");            
            out.println("</head>");
            out.println("<body>");
            if(errorMsgs!=null){
            out.println("<h2>Error Report</h2>");
            
            out.println("<font color='red'>Please correct the following errors:");
            out.println("<ul>");
            Iterator items = errorMsgs.iterator();
            while ( items.hasNext() ) {
            String message = (String) items.next();
            out.println("  <li>" + message + "</li>");
            }
            out.println("</ul>");
            out.println("Back up and try again.");
            out.println("</font>");
            }
            out.println("<div>");
            out.println("<form action='add_dvd.do' method='POST'>");
            out.println("title:<input type='text' name='title'");
            if(title==null){
            title="";
            }
            out.println("value='"+title+"'> <br/>");
            
            out.println("year:<input type='text' name='year'");
            if(title==null){
            year="";
            }
            out.println ("value='"+year+"'> <br/>");
            
            out.println("genre:<select name='genre'>");
            
            for (int i=0; i<genres.length; i++){
            out.println("                 <option value='"+genres[i]+"'");
            if(genre!=null && genre.equals(genres[i])){
            out.println("Selected");;
            }
            out.println(">"+genres[i]+"</option");
            }
            out.println("</select>");
            //*****************************************************************************
            out.println("</select> new genre:<input type='text' name='newGenre'");
            if(title==null){
            newgenre="";
            }
            out.println("value='"+newgenre+"' /><br/>");
            out.println("<br/>");
            out.println("<input type ='submit' value='enviar'/>");         
            out.println("</form>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    //Declarar arreglo String

    private String[] genres = null;
    
    //Crear metodo init
    
    @Override
    public void init() throws ServletException{
        
        String genre_list = getInitParameter("genre-list");
        if (genre_list == null){
            genre_list = "drama, acción, terror";
        }
        genres = genre_list.split(",");
    }
    
    





}
